package com.vitabuybuy.tv.ui.mine.order.viewmodel

import android.view.View
import androidx.lifecycle.MutableLiveData
import com.library.common.BuildConfig
import com.library.common.base.BaseViewModel
import com.library.common.base.LoadingDialogEntity
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.NetUrl
import com.library.common.net.error.msg
import com.library.common.net.loadingtype.LoadingType
import com.library.common.toast.XToast
import com.vitabuybuy.tv.dto.OrderDetailDto
import com.vitabuybuy.tv.dto.OrderListDto
import com.vitabuybuy.tv.network.AuthRepository
import com.vitabuybuy.tv.network.OrderRepository
import rxhttp.async
import java.util.ArrayList
import java.util.HashMap

class OrderViewModel : BaseViewModel() {

    val orderList = MutableLiveData<MutableList<OrderListDto>>()
    val orderDetail = MutableLiveData<OrderDetailDto>()

    fun getOrderList(
        status: String?,
        keyWord: String,
        pageNumber: Int,
        pageSize: Int
    ) {
        rxHttpRequest {
            onRequest = {
                val it = OrderRepository.getOrderList(status, keyWord, pageNumber, pageSize).await()
                orderList.value = it
                defUI.msgEvent.postValue(MessageModel(1, arg1 = it.size))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }

    fun cancelOrder(id: Int?) {
        rxHttpRequest {
            onRequest = {
                OrderRepository.cancelOrder(id).await()
                defUI.msgEvent.postValue(MessageModel(2, arg1 = 1))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun receiverOrder(id: Int?) {
        rxHttpRequest {
            onRequest = {
                OrderRepository.receiverOrder(id).await()
                defUI.msgEvent.postValue(MessageModel(3, arg1 = 2))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun remindOrder(id: Int?) {
        rxHttpRequest {
            onRequest = {
                OrderRepository.remindOrder(id).await()
                defUI.msgEvent.postValue(MessageModel(4))

            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun deleteOrder(id: Int?, position: Int) {
        rxHttpRequest {
            onRequest = {
                OrderRepository.deleteOrder(id).await()
                defUI.msgEvent.postValue(MessageModel(5, arg1 = position))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun getOrderDetail(id: Int?) {
        rxHttpRequest {
            onRequest = {
                orderDetail.value = OrderRepository.getOrderDetail(id).await()
                statusView.set(LoadingType.SHOW_CONTENT)
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = true
        }
    }

    fun uploadImage(paths: List<String>) {
        val images = ArrayList<String>()
        defUI.loading.value = LoadingDialogEntity(isShow = true)
        for (file in paths) {
            rxHttpRequest {
                onRequest = {
                    val it = AuthRepository.upLoadImage(file).await()
                    images.add(NetUrl.FILE_PROFILE + it)
                    if (images.size == paths.size) {
                        defUI.loading.value = LoadingDialogEntity(isShow = false)
                        defUI.msgEvent.postValue(MessageModel(6, obj = images))
                    }
                }
                onError = {
                    defUI.loading.value = LoadingDialogEntity(isShow = false)
                    XToast.normal(it.msg)
                }
            }
        }
    }

    fun uploadVideo(videoPath: String, coverPath: String) {
        rxHttpRequest {
            onRequest = {
                val video = AuthRepository.upLoadImage(videoPath).async(this)
                val image = AuthRepository.upLoadImage(coverPath).async(this)
                val videoUrl = NetUrl.FILE_PROFILE + video.await()
                val imageUrl = NetUrl.FILE_PROFILE + image.await()
                defUI.msgEvent.postValue(MessageModel(8, obj = videoUrl, msg = imageUrl))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
            loadingMessage = "上传中..."
        }
    }

    fun saveEvaluationList(mapOf: HashMap<String, Any>) {
        rxHttpRequest {
            onRequest = {
                OrderRepository.saveEvaluation(mapOf).await()
                defUI.msgEvent.postValue(MessageModel(7))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }
}
